/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controladores;

import controladores.exceptions.IllegalOrphanException;
import controladores.exceptions.NonexistentEntityException;
import controladores.exceptions.PreexistingEntityException;
import entidades.Transporteterrestre;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entidades.Transporte;
import entidades.Direccion;
import entidades.Convenios;
import entidades.Carro;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Andres
 */
public class TransporteterrestreJpaController {

    public TransporteterrestreJpaController() {
        emf = Persistence.createEntityManagerFactory("proyectoCongresoPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Transporteterrestre transporteterrestre) throws IllegalOrphanException, PreexistingEntityException, Exception {
        if (transporteterrestre.getCarroList() == null) {
            transporteterrestre.setCarroList(new ArrayList<Carro>());
        }
        List<String> illegalOrphanMessages = null;
        Transporte transporteOrphanCheck = transporteterrestre.getTransporte();
        if (transporteOrphanCheck != null) {
            Transporteterrestre oldTransporteterrestreOfTransporte = transporteOrphanCheck.getTransporteterrestre();
            if (oldTransporteterrestreOfTransporte != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The Transporte " + transporteOrphanCheck + " already has an item of type Transporteterrestre whose transporte column cannot be null. Please make another selection for the transporte field.");
            }
        }
        Direccion direccionOrphanCheck = transporteterrestre.getDireccion();
        if (direccionOrphanCheck != null) {
            Transporteterrestre oldTransporteterrestreOfDireccion = direccionOrphanCheck.getTransporteterrestre();
            if (oldTransporteterrestreOfDireccion != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The Direccion " + direccionOrphanCheck + " already has an item of type Transporteterrestre whose direccion column cannot be null. Please make another selection for the direccion field.");
            }
        }
        if (illegalOrphanMessages != null) {
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Transporte transporte = transporteterrestre.getTransporte();
            if (transporte != null) {
                transporte = em.getReference(transporte.getClass(), transporte.getCodigoEmpresa());
                transporteterrestre.setTransporte(transporte);
            }
            Direccion direccion = transporteterrestre.getDireccion();
            if (direccion != null) {
                direccion = em.getReference(direccion.getClass(), direccion.getCodigo());
                transporteterrestre.setDireccion(direccion);
            }
            Convenios convenios = transporteterrestre.getConvenios();
            if (convenios != null) {
                convenios = em.getReference(convenios.getClass(), convenios.getCodigoconvenio());
                transporteterrestre.setConvenios(convenios);
            }
            List<Carro> attachedCarroList = new ArrayList<Carro>();
            for (Carro carroListCarroToAttach : transporteterrestre.getCarroList()) {
                carroListCarroToAttach = em.getReference(carroListCarroToAttach.getClass(), carroListCarroToAttach.getPlaca());
                attachedCarroList.add(carroListCarroToAttach);
            }
            transporteterrestre.setCarroList(attachedCarroList);
            em.persist(transporteterrestre);
            if (transporte != null) {
                transporte.setTransporteterrestre(transporteterrestre);
                transporte = em.merge(transporte);
            }
            if (direccion != null) {
                direccion.setTransporteterrestre(transporteterrestre);
                direccion = em.merge(direccion);
            }
            if (convenios != null) {
                Transporteterrestre oldTransporteterrestreOfConvenios = convenios.getTransporteterrestre();
                if (oldTransporteterrestreOfConvenios != null) {
                    oldTransporteterrestreOfConvenios.setConvenios(null);
                    oldTransporteterrestreOfConvenios = em.merge(oldTransporteterrestreOfConvenios);
                }
                convenios.setTransporteterrestre(transporteterrestre);
                convenios = em.merge(convenios);
            }
            for (Carro carroListCarro : transporteterrestre.getCarroList()) {
                Transporteterrestre oldTransporteterrestreOfCarroListCarro = carroListCarro.getTransporteterrestre();
                carroListCarro.setTransporteterrestre(transporteterrestre);
                carroListCarro = em.merge(carroListCarro);
                if (oldTransporteterrestreOfCarroListCarro != null) {
                    oldTransporteterrestreOfCarroListCarro.getCarroList().remove(carroListCarro);
                    oldTransporteterrestreOfCarroListCarro = em.merge(oldTransporteterrestreOfCarroListCarro);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findTransporteterrestre(transporteterrestre.getIdTransporte()) != null) {
                throw new PreexistingEntityException("Transporteterrestre " + transporteterrestre + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Transporteterrestre transporteterrestre) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Transporteterrestre persistentTransporteterrestre = em.find(Transporteterrestre.class, transporteterrestre.getIdTransporte());
            Transporte transporteOld = persistentTransporteterrestre.getTransporte();
            Transporte transporteNew = transporteterrestre.getTransporte();
            Direccion direccionOld = persistentTransporteterrestre.getDireccion();
            Direccion direccionNew = transporteterrestre.getDireccion();
            Convenios conveniosOld = persistentTransporteterrestre.getConvenios();
            Convenios conveniosNew = transporteterrestre.getConvenios();
            List<Carro> carroListOld = persistentTransporteterrestre.getCarroList();
            List<Carro> carroListNew = transporteterrestre.getCarroList();
            List<String> illegalOrphanMessages = null;
            if (transporteNew != null && !transporteNew.equals(transporteOld)) {
                Transporteterrestre oldTransporteterrestreOfTransporte = transporteNew.getTransporteterrestre();
                if (oldTransporteterrestreOfTransporte != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The Transporte " + transporteNew + " already has an item of type Transporteterrestre whose transporte column cannot be null. Please make another selection for the transporte field.");
                }
            }
            if (direccionNew != null && !direccionNew.equals(direccionOld)) {
                Transporteterrestre oldTransporteterrestreOfDireccion = direccionNew.getTransporteterrestre();
                if (oldTransporteterrestreOfDireccion != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The Direccion " + direccionNew + " already has an item of type Transporteterrestre whose direccion column cannot be null. Please make another selection for the direccion field.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (transporteNew != null) {
                transporteNew = em.getReference(transporteNew.getClass(), transporteNew.getCodigoEmpresa());
                transporteterrestre.setTransporte(transporteNew);
            }
            if (direccionNew != null) {
                direccionNew = em.getReference(direccionNew.getClass(), direccionNew.getCodigo());
                transporteterrestre.setDireccion(direccionNew);
            }
            if (conveniosNew != null) {
                conveniosNew = em.getReference(conveniosNew.getClass(), conveniosNew.getCodigoconvenio());
                transporteterrestre.setConvenios(conveniosNew);
            }
            List<Carro> attachedCarroListNew = new ArrayList<Carro>();
            for (Carro carroListNewCarroToAttach : carroListNew) {
                carroListNewCarroToAttach = em.getReference(carroListNewCarroToAttach.getClass(), carroListNewCarroToAttach.getPlaca());
                attachedCarroListNew.add(carroListNewCarroToAttach);
            }
            carroListNew = attachedCarroListNew;
            transporteterrestre.setCarroList(carroListNew);
            transporteterrestre = em.merge(transporteterrestre);
            if (transporteOld != null && !transporteOld.equals(transporteNew)) {
                transporteOld.setTransporteterrestre(null);
                transporteOld = em.merge(transporteOld);
            }
            if (transporteNew != null && !transporteNew.equals(transporteOld)) {
                transporteNew.setTransporteterrestre(transporteterrestre);
                transporteNew = em.merge(transporteNew);
            }
            if (direccionOld != null && !direccionOld.equals(direccionNew)) {
                direccionOld.setTransporteterrestre(null);
                direccionOld = em.merge(direccionOld);
            }
            if (direccionNew != null && !direccionNew.equals(direccionOld)) {
                direccionNew.setTransporteterrestre(transporteterrestre);
                direccionNew = em.merge(direccionNew);
            }
            if (conveniosOld != null && !conveniosOld.equals(conveniosNew)) {
                conveniosOld.setTransporteterrestre(null);
                conveniosOld = em.merge(conveniosOld);
            }
            if (conveniosNew != null && !conveniosNew.equals(conveniosOld)) {
                Transporteterrestre oldTransporteterrestreOfConvenios = conveniosNew.getTransporteterrestre();
                if (oldTransporteterrestreOfConvenios != null) {
                    oldTransporteterrestreOfConvenios.setConvenios(null);
                    oldTransporteterrestreOfConvenios = em.merge(oldTransporteterrestreOfConvenios);
                }
                conveniosNew.setTransporteterrestre(transporteterrestre);
                conveniosNew = em.merge(conveniosNew);
            }
            for (Carro carroListOldCarro : carroListOld) {
                if (!carroListNew.contains(carroListOldCarro)) {
                    carroListOldCarro.setTransporteterrestre(null);
                    carroListOldCarro = em.merge(carroListOldCarro);
                }
            }
            for (Carro carroListNewCarro : carroListNew) {
                if (!carroListOld.contains(carroListNewCarro)) {
                    Transporteterrestre oldTransporteterrestreOfCarroListNewCarro = carroListNewCarro.getTransporteterrestre();
                    carroListNewCarro.setTransporteterrestre(transporteterrestre);
                    carroListNewCarro = em.merge(carroListNewCarro);
                    if (oldTransporteterrestreOfCarroListNewCarro != null && !oldTransporteterrestreOfCarroListNewCarro.equals(transporteterrestre)) {
                        oldTransporteterrestreOfCarroListNewCarro.getCarroList().remove(carroListNewCarro);
                        oldTransporteterrestreOfCarroListNewCarro = em.merge(oldTransporteterrestreOfCarroListNewCarro);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = transporteterrestre.getIdTransporte();
                if (findTransporteterrestre(id) == null) {
                    throw new NonexistentEntityException("The transporteterrestre with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Transporteterrestre transporteterrestre;
            try {
                transporteterrestre = em.getReference(Transporteterrestre.class, id);
                transporteterrestre.getIdTransporte();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The transporteterrestre with id " + id + " no longer exists.", enfe);
            }
            Transporte transporte = transporteterrestre.getTransporte();
            if (transporte != null) {
                transporte.setTransporteterrestre(null);
                transporte = em.merge(transporte);
            }
            Direccion direccion = transporteterrestre.getDireccion();
            if (direccion != null) {
                direccion.setTransporteterrestre(null);
                direccion = em.merge(direccion);
            }
            Convenios convenios = transporteterrestre.getConvenios();
            if (convenios != null) {
                convenios.setTransporteterrestre(null);
                convenios = em.merge(convenios);
            }
            List<Carro> carroList = transporteterrestre.getCarroList();
            for (Carro carroListCarro : carroList) {
                carroListCarro.setTransporteterrestre(null);
                carroListCarro = em.merge(carroListCarro);
            }
            em.remove(transporteterrestre);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Transporteterrestre> findTransporteterrestreEntities() {
        return findTransporteterrestreEntities(true, -1, -1);
    }

    public List<Transporteterrestre> findTransporteterrestreEntities(int maxResults, int firstResult) {
        return findTransporteterrestreEntities(false, maxResults, firstResult);
    }

    private List<Transporteterrestre> findTransporteterrestreEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Transporteterrestre.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Transporteterrestre findTransporteterrestre(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Transporteterrestre.class, id);
        } finally {
            em.close();
        }
    }

    public int getTransporteterrestreCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Transporteterrestre> rt = cq.from(Transporteterrestre.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
